using System;
using System.Collections.Generic;

namespace Mbs.ComponentModel
{
    [Serializable]
    public class ChangedService : DisposableObject, IChangedService
    {
        private bool _isChanged;
        private bool _isFireAcceptedChangedEvent = true;
        private long _changedServiceCount = 0;
        private List<IChangedService> services;
        public event EventHandler AcceptedChanged;

        public ChangedService()
        {
        }

        //在SuspendChanged之后，恢复操作，且必须成对出现
        public void ResumeChanged()
        {
            _changedServiceCount--;
            if (services != null)
                foreach (ChangedService item in services)
                    item._changedServiceCount--;
        }
        //挂起变化的后续事件操作
        public void SuspendChanged()
        {
            _changedServiceCount++;
            if (services != null)
                foreach (ChangedService item in services)
                    item._changedServiceCount++;
        }

        public void MarkChanged()
        {
            if (_changedServiceCount <= 0)
            {
                _isChanged = true;
            }
        }

        protected void OnAcceptedChanged(object sender, EventArgs e)
        {
            if (AcceptedChanged != null)
                AcceptedChanged(sender, e);
        }

        public bool HasChanged
        {
            get
            {
                return _isChanged;
            }
        }

        public void AcceptChanged()
        {
            _isChanged = false;
            if (_isFireAcceptedChangedEvent)
                OnAcceptedChanged(this, EventArgs.Empty);
            if (services != null)
                foreach (ChangedService item in services)
                    item._isChanged = false;
        }

        public void AddService(IChangedService service)
        {
            if (service == null)
                return;
            if (services == null)
                services = new List<IChangedService>();
            if (service is ChangedService)
                (service as ChangedService)._isFireAcceptedChangedEvent = false;

            services.Add(service);
        }

        public void RemoveService(IChangedService service)
        {
            if (service == null)
                return;
            if (services == null)
                return;
            if (!services.Contains(service))
                return;
            services.Remove(service);
            if (services.Count == 0)
                services = null;
        }


        protected override void Dispose(bool disposing)
        {
            base.Dispose(disposing);

            if (services != null)
            {
                foreach (ChangedService item in services)
                {
                    item.Dispose();
                }
                services.Clear();
                services = null;
            }
        }
    }
}
